package handler;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import service.TestService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AddEntity implements Controller {
    private TestService service;

    public void setService(TestService service) {
        this.service = service;
    }

    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        String methodValue = httpServletRequest.getMethod();
        ModelAndView modelAndView = new ModelAndView("index");
        if (methodValue.equals("POST")) {
            String field = httpServletRequest.getParameter("param");
            boolean status = service.add(field);
            // 若使用addObject(Object)添加对象，那么属性名称为对象的非限定类名（首字母为小写）
            modelAndView.addObject(status);
        } else {
            modelAndView.addObject("boolean", false);
        }
        return modelAndView;
    }
}
